package com.youfang.sixthmedia.calc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.gzit.utils.AndroidUtils;
import com.gzit.utils.StringUtils;
import com.youfang.sixthmedia.R;
import com.youfang.sixthmedia.calc.YoufangCalc.DEBJ;
import com.youfang.sixthmedia.calc.YoufangCalc.DEBX;

public class CalcInputActivity extends Activity {

	String type;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.calc_input_activity);

		Intent intent = this.getIntent();

		type = intent.getStringExtra("type");

		((TextView) this.findViewById(R.id.title))
				.setText("debj".equals(type) ? "等额本金还款法" : "等额本息还款法");

		this.findViewById(R.id.calcBtn).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {

						String count = ((TextView) findViewById(R.id.count))
								.getText().toString();

						String rate = ((TextView) findViewById(R.id.rate))
								.getText().toString();

						String month = ((TextView) findViewById(R.id.month))
								.getText().toString();

						if (StringUtils.isBlank(count)) {
							AndroidUtils.toastShort("贷款额度不能为空");
							return;
						}

						if (StringUtils.isBlank(rate)) {
							AndroidUtils.toastShort("贷款月利率不能为空");
							return;
						}

						if (StringUtils.isBlank(month)) {
							AndroidUtils.toastShort("贷款时长不能为空");
							return;
						}

						if ("debj".equals(type)) {
							DEBJ data = new YoufangCalc.DEBJ().calc(
									Double.parseDouble(count),
									Double.parseDouble(rate),
									Integer.parseInt(month));
							gotoDEBJ(data);

						} else {
							DEBX data = new YoufangCalc.DEBX().calc(
									Double.parseDouble(count),
									Double.parseDouble(rate),
									Integer.parseInt(month));
							gotoDEBX(data);
						}

					}
				});

	}

	public void gotoDEBX(DEBX debx) {

		Intent intent = new Intent();
		intent.putExtra("type", "debx");
		intent.putExtra("data", debx);
		intent.setClass(this, CalcResultActivity.class);
		this.startActivity(intent);

	}

	public void gotoDEBJ(DEBJ debj) {
		Intent intent = new Intent();
		intent.putExtra("type", "debj");
		intent.putExtra("data", debj);
		intent.setClass(this, CalcResultActivity.class);
		this.startActivity(intent);
	}

}
